package com.sikaryofficial.common.core.valid;

/**
 * @author : qinjinyuan
 * @desc : TODO  请填写你的功能描述
 * @date : 2025/04/17 8:50
 */

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;

public class ConditionalLengthValidator implements ConstraintValidator<ConditionalLength, String> {

    private int min;
    private int max;

    @Override
    public void initialize(ConditionalLength constraintAnnotation) {
        this.min = constraintAnnotation.min();
        this.max = constraintAnnotation.max();
    }

    @Override
    public boolean isValid(String value, ConstraintValidatorContext context) {
        if (value == null || value.isEmpty()) {
            return true; // 如果字段为空，则不进行长度校验
        }
        return value.length() >= min && value.length() <= max;
    }
}